package com.dz.scrm.utils

import io.reactivex.ObservableTransformer
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import me.jessyan.rxerrorhandler.handler.RetryWithDelay

/**
 * 作者：fanda
 * 日期：2021/4/21 19:51
 * 描述：放置便于使用 RxJava 的一些工具方法
 */

fun <T> io_main_retry(): ObservableTransformer<T,T>{
    return ObservableTransformer {
        upstream ->
                upstream.subscribeOn(AndroidSchedulers.mainThread())
                        .subscribeOn(Schedulers.io())
                        .retryWhen(RetryWithDelay(3,2))//遇到错误时重试,第一个参数为重试几次,第二个参数为重试的间隔
                        .unsubscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
    }
}

fun <T> io_main(): ObservableTransformer<T,T>{
    return ObservableTransformer {
        upstream ->
        upstream.subscribeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
    }
}